package com.ldk.base.framework.datasource;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.ibm.db2.jcc.DB2DataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import oracle.jdbc.pool.OracleDataSource;

/**
 * NoXA DataSource数据工厂
 * 支持ORACLE MYSQL MSSQL POSTRESQL 数据库
 * @author xiangwl
 *
 */
public class CustomNoXADataSourceFactory extends BaseDataSourceFacotory{
	/**
	 * 创建数据库
	 * @param driver
	 * @param user
	 * @param password
	 * @return
	 */
	public static DataSource createDataSource(DataSourceDriver driver, String serverName, int portNumber,String databaseName,
			  									String user, String password){
		String url = createDbUrl(driver, serverName, portNumber, databaseName);
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		DataSource datasource = null;
		try{
			if(driver == DataSourceDriver.ORACLE){
				OracleDataSource oracleDataSource = new OracleDataSource();
				oracleDataSource.setURL(url);
				oracleDataSource.setUser(user);
				oracleDataSource.setPassword(password);
				
				datasource = oracleDataSource;
			}
			else if(driver == DataSourceDriver.MYSQL){
				MysqlDataSource mysqlDataSource = new MysqlDataSource();
				mysqlDataSource.setUrl(url);
				mysqlDataSource.setUser(user);
				mysqlDataSource.setPassword(password);
				
				datasource = mysqlDataSource;
			}
			else if(driver == DataSourceDriver.MSSQL){
				
				SQLServerDataSource sqlServerDataSource = new SQLServerDataSource();
				sqlServerDataSource.setURL(url);
				sqlServerDataSource.setUser(user);
				sqlServerDataSource.setPassword(password);
				
				datasource = sqlServerDataSource;
			}
			else if(driver == DataSourceDriver.POSTRESQL){
				PGSimpleDataSource pgSimpleDataSource = new PGSimpleDataSource();
				pgSimpleDataSource.setUrl(url);
				pgSimpleDataSource.setUser(user);
				pgSimpleDataSource.setPassword(password);
				
				datasource = pgSimpleDataSource;
			}
			else if(driver == DataSourceDriver.DB2){
				DB2DataSource db2DataSource = new DB2DataSource();
				db2DataSource.setServerName(serverName);
				db2DataSource.setPortNumber(portNumber);
				db2DataSource.setDatabaseName(databaseName);
				db2DataSource.setUser(user);
				db2DataSource.setPassword(password);
				//driver type 4
				db2DataSource.setDriverType(4);
				db2DataSource.setCurrentSchema(databaseName);
				
				datasource = db2DataSource;
			}
			else{
				BasicDataSource basicDataSource = new BasicDataSource();
				basicDataSource.setDriverClassName(driver.getNoXADriver());
				basicDataSource.setUrl(url);
				basicDataSource.setUsername(user);
				basicDataSource.setPassword(password);
				basicDataSource.setMinIdle(5);
				basicDataSource.setMaxIdle(20);
				basicDataSource.setMaxActive(100);
				basicDataSource.setMaxWait(100000);
				basicDataSource.setDefaultAutoCommit(false);
				basicDataSource.setRemoveAbandoned(true);
				basicDataSource.setRemoveAbandonedTimeout(600);
				basicDataSource.setTestWhileIdle(true);
				basicDataSource.setTimeBetweenEvictionRunsMillis(60000);
				basicDataSource.setNumTestsPerEvictionRun(20);
				basicDataSource.setMinEvictableIdleTimeMillis(30000);
				
				datasource = basicDataSource;
			}
			if(datasource != null)
				transactionManager.setDataSource(datasource);
		}
		catch(Exception e){
			
		}
		
		return datasource;
	}
}
